﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Algobox.Structure.Finance.Prices;

namespace Algobox.Objects.Finance.Assets.Stock
{
    public class StockListing : IStock
    {
        /// <summary>
        /// This is an exchange listing of a stock security
        /// </summary>
        /// <param name="security">The security to which this listing is based on</param>
        /// <param name="exchange">Exchange which this stock is listed</param>
        /// <param name="tickSize">Tick size to be used by this listing</param>
        /// <param name="priority">Smaller number means higher priority</param>
        public StockListing(StockSecurity security, Exchanges.ExchangeMIC exchange, ITick tickSize, int priority = 1)
        {
            Symbol = security.Symbol;
            Exchange = exchange;
            Security = security;
            TickSize = tickSize;
            Prices = new StockWatch(this);
            _id = security.Symbol + ":" + exchange.ToString() + ":" + security.Currency.ToString();
        }

        private readonly string _id;

        public string Symbol { get; private set; }
        public Exchanges.ExchangeMIC Exchange { get; private set; }
        public StockSecurity Security { get; private set; }

        public string Bloomberg { get; set; }
        public string Reuters { get; set; }

        public ITick TickSize { get; set; }

        public StockWatch Prices { get; set; }

        /// <summary>
        /// When attempting to order a list of StockListings Priority is used with a smaller number meaning higher priority
        /// </summary>
        public int Priority { get; set; }

        Phases.Phase _phase;
        Object _phaseLock = new object();
        public Phases.Phase Phase 
        {
            get { return _phase; }
            set 
            {
                lock (_phaseLock)
                {
                    if (_phase != value)
                    {
                        _phasePrevious = _phase;
                        _phase = value;
                    }
                }
            }
        }
        
        Phases.Phase _phasePrevious;        
        public Phases.Phase PhasePrevious { get { return _phasePrevious; } }

        public override string ToString()
        {
            return _id;
        }
    }
}
